package com.gyms.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.classic.okhttp.Cons;
import com.classic.okhttp.beans.HVProductInfoBean;
import com.gyms.R;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import constant.PicSplit;
import glide.ImageLoaderUtils;
import myInterface.OnItemClickLitener;
import utils.DecimalUtils;
import utils.StringUtils;

/**
 * 所有课程的适配器 recyclerView适配器 ltx
 */
public class AllCourseRecyClerAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {
    private List<HVProductInfoBean> mProductInfoBeans;

    private Context mContext;
    private LayoutInflater layoutInflater;
    private OnItemClickLitener mOnItemClickLitener;//Item的点击监听
    private static final int S_HEAD=1;
    public static final int S_STICK_HEAD=2;
    private static final int S_CONTENT=3;
    private View mHeadView;
    private View mHeaderStick;



    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public void setHeadView(View mHeadView){
        this.mHeadView=mHeadView;
    }
    public void setHeaderStick(View mHeaderStick){
        this.mHeaderStick=mHeaderStick;
    }

    public int getHeadPos(){
        int mHeadViewCount=mHeadView==null?0:1;
        int mHeadStickCount=mHeaderStick==null?0:1;
       return mHeadViewCount+mHeadStickCount;
    }

    public AllCourseRecyClerAdapter(Context context, List<HVProductInfoBean> list) {
        this.mContext = context;
        mProductInfoBeans = list;
        this.layoutInflater = LayoutInflater.from(context);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder mHolder=null;
        switch (viewType){
            case S_HEAD:
                mHolder=new VHCoureseHead(mHeadView);
                break;

            case S_STICK_HEAD:
                mHolder=new VHStickHead(mHeaderStick);
                break;
            case S_CONTENT:
                View view = layoutInflater.inflate(R.layout.item_recy_all_course, parent, false);
                mHolder= new VhCourseContent(view);
                break;
        }
        return mHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        int mItemType=getItemViewType(position);
        if(mItemType==S_HEAD||mItemType==S_STICK_HEAD){
            return;
        }

        HVProductInfoBean mProductBean=mProductInfoBeans.get(position-getHeadPos());
        final VhCourseContent mVHCourseContent= (VhCourseContent) viewHolder;
        if(!StringUtils.isNull(mProductBean)){
            initializeViews(mProductBean, mVHCourseContent);
        }
        //如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            mVHCourseContent.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(mVHCourseContent.itemView, position-getHeadPos());
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && mHeadView != null) {
            return S_HEAD;
        } else if (position == 1 && mHeaderStick != null) {
            return S_STICK_HEAD;
        }
        return S_CONTENT;
    }

    @Override
    public int getItemCount() {
        return null == mProductInfoBeans ? getHeadPos() : mProductInfoBeans.size()+getHeadPos();
    }


    private void initializeViews(HVProductInfoBean object, VhCourseContent mViewHolder) {
        if(!StringUtils.isNull(object)) {
            if(!StringUtils.isNull(object.getDiscount())){//优惠类不为null
                String discountUrl=object.getDiscount().get(0).getDiscountIcon();
                String mDiscountContent=object.getDiscount().get(0).getDiscountContent();//优惠文本
                if(!StringUtils.isNull(discountUrl)){//优惠的图片为null隐藏
                    mViewHolder.mTvDiscount.setVisibility(View.VISIBLE);
                    mViewHolder.tvProductHui.setVisibility(View.VISIBLE);
//                    ImageLoaderUtils.displayBigPhoto(mContext, mViewHolder.mTvDiscount, discountUrl);
                    ImageLoaderUtils.display(mContext,mViewHolder.mTvDiscount,  PicSplit.urlAdd(discountUrl,PicSplit.TYPE_TAB));

                }else{
                    mViewHolder.mTvDiscount.setVisibility(View.GONE);//icon
                    mViewHolder.tvProductHui.setVisibility(View.GONE);//优惠文本

                }
                mViewHolder.tvProductHui.setText(StringUtils.CS(mDiscountContent));
            }else{
                mViewHolder.mTvDiscount.setVisibility(View.GONE);
                mViewHolder.tvProductHui.setVisibility(View.GONE);
            }

            if(!object.refundable&&StringUtils.isNull(object.getDiscount())){//不可退款并且 优惠类为null 就隐藏
                mViewHolder.mLlDiscound.setVisibility(View.GONE);
            }else {
                mViewHolder.mLlDiscound.setVisibility(View.VISIBLE);
            }

            //是否可退款
                mViewHolder.mIvRefund.setVisibility(object.refundable?View.VISIBLE:View.GONE);
                mViewHolder.mTvRefundText.setVisibility(object.refundable?View.VISIBLE:View.GONE);
                if(object.refundable){
                    ImageLoaderUtils.display(mContext,mViewHolder.mIvRefund,  PicSplit.urlAdd(Cons.S_PRODUCT_REFUND_ICON,PicSplit.TYPE_TAB));
                }

            mViewHolder.tvProductTitle.setText(StringUtils.CS(object.getProductName()));
            mViewHolder.tvAddressDetil.setText(StringUtils.CS(object.getShortAddress()));
            if(!StringUtils.isNull(object.getMinPrice())&&!StringUtils.isNull(object.getMaxPrice())) {
                if (object.getMinPrice().compareTo(object.getMaxPrice()) == 0) {
                    mViewHolder.tvPrise.setText( DecimalUtils.setPriceStyle(mContext,object.getMinPrice(),R.dimen.price_text_size_14,R.dimen.price_text_size_12,true));
                } else {
                    mViewHolder.tvPrise.setText(DecimalUtils.setPriceStyle(mContext,object.getMinPrice(),R.dimen.price_text_size_14,R.dimen.price_text_size_12,true).append(" - ").append( DecimalUtils.setPriceStyle(mContext,object.getMaxPrice(),R.dimen.price_text_size_14,R.dimen.price_text_size_12,false)));
                }
            }
            if(!StringUtils.isNull(object.getDistance())) {
                double dis = object.getDistance() / 1000;
                showDis(mViewHolder,dis);
            }

            boolean isCollection=object.getIsCollection();//收藏
            boolean isRecommend=object.getIsRecommend();//推荐
            if(!isCollection&&!isRecommend){
                mViewHolder.ivRecommend.setVisibility(View.GONE);
            }else{
                mViewHolder.ivRecommend.setVisibility(View.VISIBLE);
            }
            if (isRecommend) {
                mViewHolder.ivRecommend.setImageResource(R.mipmap.icon_best);
            }
            if (isCollection) {//收藏大于推荐
                mViewHolder.ivRecommend.setImageResource(R.mipmap.icon_save_subscript);
            }

            ImageLoaderUtils.display(mContext, mViewHolder.ivProductCover, object.getProductUrl());
        }
    }

    /**
     * 小于1公里的，距离显示“<1km”；大于等于1公里，小于等于50公里的，距离显示“xx.xkm”；大于50公里的，距离显示“>50km”；
     *
     * @param dis 公里
     */
    private void showDis(VhCourseContent mViewHolder,double dis) {
        String disText;
        if (dis < 1) {
            mViewHolder.tvDistance.setText("<1km");
        } else if (dis >= 1 && dis <= 50) {
            disText=DecimalUtils.getDistaceText(dis);
            mViewHolder.tvDistance.setText(disText + "km");
        } else {
            mViewHolder.tvDistance.setText(">50km");
        }
    }
    private  class VhCourseContent extends RecyclerView.ViewHolder  {
        ImageView ivProductCover;
        TextView tvProductTitle;
        TextView tvAddressDetil;
        TextView tvDistance;
        TextView tvProductHui;
        TextView tvPrise;
        ImageView ivRecommend;
        ImageView mTvDiscount;//优惠
        LinearLayout mLlDiscound;//优惠
        ImageView mIvRefund;//可退款图标
        TextView mTvRefundText;
        VhCourseContent(View view) {
            super(view);
             ivProductCover= (ImageView) view.findViewById(R.id.iv_product_cover);
            tvProductTitle= (TextView) view.findViewById(R.id.tv_product_title);
            tvAddressDetil= (TextView) view.findViewById(R.id.tv_address_detil);
            tvDistance= (TextView) view.findViewById(R.id.tv_distance);
            tvProductHui= (TextView) view.findViewById(R.id.tv_product_hui);
            tvPrise= (TextView) view.findViewById(R.id.tv_prise);
            ivRecommend= (ImageView) view.findViewById(R.id.iv_recommend);
            mTvDiscount= (ImageView) view.findViewById(R.id.tv_discount);
            mLlDiscound= (LinearLayout) view.findViewById(R.id.ll_discound);
            mIvRefund= (ImageView) view.findViewById(R.id.iv_refund);
            mTvRefundText= (TextView) view.findViewById(R.id.tv_refund_text);
            AutoUtils.autoSize(view);

        }
    }

    private class VHCoureseHead extends RecyclerView.ViewHolder{

        public VHCoureseHead(View itemView) {
            super(itemView);
        }
    }

    private class VHStickHead extends RecyclerView.ViewHolder{

        public VHStickHead(View itemView) {
            super(itemView);
        }
    }

}
